﻿//**************************************************
//Description   :   数据管理类
//Author        :   leubooth
//Date          :   2015.09.02
//**************************************************

using System.IO;
using System.Text;

public class DataManager
{
    /// <summary>
    /// 读取文本文件
    /// </summary>
    /// <param name="path">文件夹路径</param>
    /// <param name="fileName">文件名称</param>
    /// <returns>文本内容</returns>
    public static string Read(string path)
    {
        //TODO...文件抛异常处理
        string pathTmp = path;
        try
        {
            StreamReader sr = new StreamReader(pathTmp, Encoding.UTF8);
            string getStr = sr.ReadToEnd();
            sr.Close();
            return getStr;
        }
        catch
        {
            return "";
        }
        
        
    }
    /// <summary>
    /// 删除文本文件
    /// </summary>
    /// <param name="path"></param>
    /// <param name="fileName"></param>
    public static void Delete(string path, string fileName){
        string pathTmp = path + "\\" + fileName + ".txt";
        File.Delete(pathTmp);
        
    }

    /// <summary>
    /// 写入文本文件
    /// </summary>
    /// <param name="value">存储内容</param>
    /// <param name="path">文件夹路径</param>
    /// <param name="fileName">文件名称</param>
    public static void Write(string value, string path,string fileName)
    {
        CreateDirectory(path);
        string pathTmp = path + "/" + fileName + ".txt";
        File.WriteAllText(pathTmp, value, Encoding.UTF8);
		//NGUIDebug.Log ("文件存储成功"+pathTmp);
    }
    
    /// <summary>
    /// 检查并创建目录
    /// </summary>
    /// <param name="path">目录路径</param>
    /// <returns>是否成功创建目录</returns>
    public static bool CreateDirectory(string path)
    {
        if (!Directory.Exists(path))
        {
            try
            {
                Directory.CreateDirectory(path);
                return true;
            }
            catch
            {
                return false;
            }
        }
        else
        {
            return true;
        }
    }
}